// stores/columns.js
import { defineStore } from 'pinia';
import { getParentColumnsService, getChildrencolumnsService } from '@/api/user.js';

export const useColumnsStore = defineStore('columns', {
  state: () => ({
    parentColumns: [],
    childrenColumns: [], // 新增属性存储子栏目
    selectedColumnId: null,
    selectedParentColumn: null,
  }),
  actions: {
    async fetchParentColumns() {
      try {
        const response = await getParentColumnsService();
        this.parentColumns = response.data.filter(column => column.parentId === null);
      } catch (error) {
        console.error('获取父栏目失败:', error);
      }
    },
    async fetchChildrenColumns(parentId) {
      try {
        const response = await getChildrencolumnsService(parentId);
        this.childrenColumns = response.data;
      } catch (error) {
        console.error('获取子栏目失败:', error);
      }
    },
    selectParentColumn(parentColumn) {
      this.selectedParentColumn = parentColumn;
    },
    selectColumn(columnId) {
      this.selectedColumnId = columnId;
    }
  },
});